﻿using System.Collections.Generic;
using Dou.UI;
using UnityEngine;

namespace Simple
{
    public class Test_SimpleList : MonoBehaviour
    {
        public List<Test_ListItemData> datas = new List<Test_ListItemData>();
        
        public UIDuplicator duplicator;

        private void Awake()
        {
            // 填充数据
            for (int i = 0; i < 10; i++)
            {
                var item = new Test_ListItemData();
                item.text = (i + 1).ToString();
                for (int j = 0; j < 10; j++)
                {
                    var reward = new Test_ListItemRewardData();
                    reward.pic = j % 2 == 0 ? "tex_lt_icon_biaoqing_01" : "tex_lt_icon_biaoqing_02";
                    reward.hasEffect = j % 2 == 0;
                    item.rewardList.Add(reward);
                }
                datas.Add(item);
            }
            
            duplicator.onItemInit += DuplicatorOnItemInit;
            duplicator.onItemUpdate += DuplicatorOnItemUpdate;
            duplicator.onItemDisable += DuplicatorOnItemDisable;
            duplicator.count = datas.Count;
        }

        private void DuplicatorOnItemInit(GameObject go, int index)
        {
            // 这里添加事件监听
        }

        private void DuplicatorOnItemUpdate(GameObject go, int index)
        {
            // 为了方便UI节点这里直接用 Find 来查找了
            var text = go.transform.Find("Lab").GetComponent<DTextMeshProUGUI>();
            var dup = go.transform.Find("RewardList").GetComponent<UIDuplicator>();
            
            // 获取数据
            var data = datas[index];
            text.text = data.text;
            
            // 第二级的列表就不能使用事件监听来实现了，获取不了对应的数据，这里直接暴力创建和获取
            dup.count = data.rewardList.Count;
            for (int i = 0; i < dup.count; i++)
            {
                var itemGO = dup.GetItem(i);
                var itemData = data.rewardList[i];
                
                // 为了方便UI节点这里直接用 Find 来查找了
                var image = itemGO.transform.Find("Image").GetComponent<DImage>();
                var eff = itemGO.transform.Find("Eff").gameObject;

                // image.spriteName = itemData.pic;
                eff.SetActive(itemData.hasEffect);
            }
        }

        private void DuplicatorOnItemDisable(GameObject go, int index)
        {
            // 这里移除事件监听
        }
    }
    
    public class Test_ListItemData
    {
        public string text;
        public List<Test_ListItemRewardData> rewardList = new List<Test_ListItemRewardData>();
    }

    public class Test_ListItemRewardData
    {
        public string pic;
        public bool hasEffect;
    }
}
